/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <div>
      <div class="fixed-bar">
        <div class="item-title">
          <router-link
            class="back"
            :to="{name:'member_account',params:{id: this.$route.params.id}}"
            title="返回列表"
          >
            <i class="fa fa-arrow-circle-o-left"></i>
          </router-link>
          <div class="subject">
            <h3>会员管理 - 资金调节</h3>
            <h5>网站系统会员管理</h5>
          </div>
        </div>
      </div>
      <form :model="account">
        <div class="ncap-form-default">
          <dl class="row">
            <dt class="tit">
              <label>金额</label>
            </dt>
            <dd class="opt">
              <select v-model="typeMoney">
                <option value="1">增加</option>
                <option value="0">减少</option>
              </select>
              <input 
			    @focus="clear(1)" 
				v-model="account.user_money" 
				@paste="account.user_money = account.user_money.replace(/[^\d.]/g,'')"
				@keyup="account.user_money = account.user_money.replace(/[^\d.]/g,'')"
				type="text" 
				class="input-txt" />
              可用余额：{{user_info.user_money}}￥
              <p class="err-msg" v-if="errResult.user_money">{{errResult.user_money}}</p>
              <p class="notic">单位元</p>
            </dd>
          </dl>
          <dl class="row">
            <dt class="tit">
              <label>积分</label>
            </dt>
            <dd class="opt">
              <select v-model="typePoints">
                <option value="1">增加</option>
                <option value="0">减少</option>
              </select>
              <input 
				v-model="account.pay_points" 
				type="text" 
				@keyup="account.pay_points=account.pay_points.replace(/^(0+)|[^\d]+/g,'')"
				class="input-txt" />
              可用积分：{{user_info.pay_points}}
              <p
                class="err-msg"
                v-if="errResult.pay_points && !account.pay_points"
              >{{errResult.pay_points}}</p>
              <p class="notic">整数</p>
            </dd>
          </dl>
          <dl class="row">
            <dt class="tit">
              <label>冻结资金</label>
            </dt>
            <dd class="opt">
              <select v-model="typeFrozen">
                <option value="1">增加冻结资金</option>
                <option value="0">减少冻结资金</option>
              </select>
              <input
			    @focus="clear(0)"
                v-model="account.frozen_money"
                type="text"
                @paste="account.frozen_money = account.frozen_money.replace(/[^\d.]/g,'')"
                @keyup="account.frozen_money = account.frozen_money.replace(/[^\d.]/g,'')"
                class="input-txt"
              />
              冻结资金：{{user_info.frozen_money}}
              <p class="err-msg" v-if="errResult.frozen_money">{{errResult.frozen_money}}</p>
              <p class="notic">单位元, 当操作冻结资金时，金额那一栏不用填写数值。</p>
            </dd>
          </dl>
          <dl class="row">
            <dt class="tit">
              <label>操作备注</label>
            </dt>
            <dd class="opt">
              <textarea
                v-model="account.desc"
                placeholder
                name="description"
                rows="6"
                class="tarea"
              ></textarea>
			  <p class="err-msg" v-if="errResult.desc">{{errResult.desc}}</p>
              <p class="notic">请输入操作备注</p>
            </dd>
          </dl>

          <div class="bot">
            <el-button type="primary" @click="onSubmit()" :loading="isLoading">确认提交</el-button>
          </div>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
import { getUser, addUserAccountLog } from "@/utils/api";
export default {
  data() {
    return {
	  account: {
		  user_money: 0,
		  frozen_money: 0,
		  pay_points: 0
	  },
	  user_info: {},
      typeMoney: "1",
      typePoints: "1",
      typeFrozen: "1",
      isLoading: false,
      errResult: {}
    };
  },
  created() {
    this.getMemberAccount();
  },
  methods: {
	clear(e) {
		if (e) {
			this.account.frozen_money = 0;
		} else {
			this.account.user_money = 0;
		}
	},
    getMemberAccount() {
      var that = this;
      getUser({
        user_id: that.$route.params.id
      }).then(function(res) {
        that.user_info = res;
      });
    },
    onSubmit() {
      var that = this;
	  if (this.typeMoney == "0") {
		  this.account.user_money = 0 - this.account.user_money;
	  }
	  if (this.typePoints == "0") {
		  this.account.pay_points = 0 - this.account.pay_points;
	  } else {
		  this.account.pay_points = this.account.pay_points ? this.account.pay_points : 0;
	  }
	  if (this.typeFrozen == "0") {
		  this.account.frozen_money = 0 - this.account.frozen_money;
	  }
	  this.account.user_id = this.user_info.user_id;
      this.isLoading = true;
      addUserAccountLog(that.account).then(function(res) {
        if (res.status == 1) {
          that.$message.success({
            message: "保存成功",
            duration: 1000,
            onClose: function() {
              that.$router.push({
                name: "member_account",
                params: {
                  id: that.account.user_id
                }
              });
            }
          });
        } else if (res.status == -1) {
          that.$message.error("参数错误");
          that.errResult = res.result;
        } else {
          that.$message.error("操作失败:" + res.msg);
        }
        that.isLoading = false;
      });
    }
  }
};
</script>

<style scoped>
	.txt, select, .vmiddle {
	    vertical-align: middle;
	    height: 26px;
	    margin-top: -3px;
	}
</style>
